﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using DGM.Objects.UClassify;
using DGM.Util.Helpers.Enums;
using DGM.Util.Helpers.Json;
using DGM.Util.Helpers.String;
using Newtonsoft.Json.Linq;

namespace DGM.Util.Helpers.UClassify
{
    public static class UClassifyHelper
    {
        public static string ReadApiKey = "tuST1HtcYJPQLwK1wdRxwTRvcGw";

        //public static const string RequestStringTypeText = "ClassifyText";
        //public static const string RequestStringTypeText = "ClassifyUrl";

        public static UClassifyGender AnaliseGender(string text)
        {
            string data = AnaliseText(CallType.Gender, text);

            JObject jsonData = JObject.Parse(data);

            UClassifyGender result = JsonHelper.GetWellKnownObjectFromJson<UClassifyGender>(jsonData);

            return result;
        }

        public static UClassifyTextLanguage AnalyseLanguage(string text)
        {
            string data = AnaliseText(CallType.TextLanguage, text);

            JObject jsonData = JObject.Parse(data);

            UClassifyTextLanguage result = JsonHelper.GetWellKnownObjectFromJson<UClassifyTextLanguage>(jsonData);

            return result;
        }

        public static UClassifySentiment AnaliseSentiment(string text)
        {
            string data = AnaliseText(CallType.Sentiment, text);

            JObject jsonData = JObject.Parse(data);

            UClassifySentiment result = JsonHelper.GetWellKnownObjectFromJson<UClassifySentiment>(jsonData);

            return result;
        }

        public static UClassifyMood AnaliseMood(string text)
        {
            string data = AnaliseText(CallType.Mood, text);

            JObject jsonData = JObject.Parse(data);

            UClassifyMood result = JsonHelper.GetWellKnownObjectFromJson<UClassifyMood>(jsonData);

            return result;
        }

        private static string AnaliseText(CallType calltype, string text)
        {
            string encodeText = HttpUtility.UrlEncode(text);
            string makerString = EnumHelper.GetDescription(GetMaker(calltype));
            string callTypeString = EnumHelper.GetDescription(calltype);
            string requestTypeString = EnumHelper.GetDescription(GetReqeustType(text));
            string requestTypeValue = GetDataType(GetReqeustType(text));

            string urlString = string.Format("http://uclassify.com/browse/{0}/{1}/{2}?readkey={3}&{4}={5}&output=json&version=1.01",
                makerString, callTypeString, requestTypeString, ReadApiKey, requestTypeValue, encodeText);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlString);
            webRequest.Timeout = -1;
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

            StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());

            string data = "";

            while (responseStream.EndOfStream == false)
            {
                data = data + responseStream.ReadLine();
            }

            return data;
        }

        private static RequestType GetReqeustType(string text)
        {
            if (RegexHelper.IsUrl(text))
            {
                return RequestType.Url;
            }
            else
            {
                return RequestType.Text;
            }
        }

        private static Maker GetMaker(CallType callType)
        {
            switch (callType)
            {
                case CallType.Mood:
                    return Maker.prfekt;
                case CallType.Sentiment:
                    return Maker.uClassify;
                    
            }
            return Maker.uClassify;
        }

        private static string GetDataType(RequestType requestType)
        {
            switch (requestType)
            {
                case RequestType.Text:
                    return "text";
                case RequestType.Url:
                    return "url";
            }
            return "text";
        }
    }

    

    public enum CallType
    {
        [Description("Sentiment")]
        Sentiment,
        [Description("Mood")]
        Mood,
        [Description("Text Language")]
        TextLanguage,
        [Description("GenderAnalyzer_v5")]
        Gender
    }

    public enum Maker
    {
        [Description("prfekt")]
        prfekt,
        [Description("uClassify")]
        uClassify
    }

    public enum RequestType
    {
        [Description("ClassifyUrl")]
        Url,
        [Description("ClassifyText")]
        Text
    }
}
